<template>
  <lodop-print-designer ref="printDesigner" :temp-value="value" :widget-options="widgets" @save="handleSave" v-if="showDesigner"/>
</template>
<script>
import { OutStockOptions, InStockOptions } from './data/options'
import { setTokenSqy } from '@/utils/auth'
import { getAgenDymb, queryAgDyMbPz, updateAgenDymb } from '@/api/pms/api'
import { decrypt } from '@/utils/crypto'
export default {
  data() {
    return {
      showDesigner: false,
      agmbid: '',
      index: 0,
      value: {},
      widgets: [],
      agenDymb:{}
    }
  },

  created() {
    const token = this.$route.query.token
    if (typeof token === 'undefined' || token === '') {
      this.$alert('请求参数错误[1]', '错误', {
        confirmButtonText: '关闭',
        callback: action => {
          window.close()
        }
      })
    }
    const params = this.$route.query.params
    if (typeof params === 'undefined' || params === '') {
      this.$alert('请求参数错误[2]', '错误', {
        confirmButtonText: '关闭',
        callback: action => {
          window.close()
        }
      })
    }
    const value = JSON.parse(decrypt({ yxw: params }))
    if (typeof value.agmbid === 'undefined' || value.agmbid === '') {
      this.$alert('请求参数错误[3]', '错误', {
        confirmButtonText: '关闭',
        callback: action => {
          window.close()
        }
      })
    }
    setTokenSqy(token)
    this.agmbid = value.agmbid
    this.getAgenDymb()
    /* try {
      let tempList = JSON.parse(localStorage.getItem('tempList')) || []
      this.value = tempList[this.index]
      this.widgets = this.value.type == 1 ? OutStockOptions : InStockOptions
    } catch (err) {
      console.error(err)
    } */
  },
  methods: {
    getAgenDymb() {
      this.loading = true
      const param = {agmbid: this.agmbid }
      getAgenDymb(param).then(response => {
        console.log('getAgenDymb',response)
        this.loading = false
        this.agenDymb = response.result
        let value = {
          title: response.result.mbname,
          width: response.result.zzkd,
          height: response.result.zzgd,
          pageWidth: response.result.zzkd,
          pageHeight: response.result.zzgd,
          tempItems:JSON.parse(response.result.cont)
        }
        this.value = value
        this.queryAgDyMbPz()
      })
    },
    queryAgDyMbPz() {
      this.loading = true
      const param = { djlx:this.agenDymb.djlx,zdjlx:this.agenDymb.zdjlx,mblx:'3' }
      queryAgDyMbPz(param).then(response => {
        console.log('queryAgDyMbPz',response)
        this.loading = false
        debugger
        let dypzx = JSON.parse(response.result.dypzx)
        debugger
        this.widgets  = dypzx
        this.showDesigner = true
      })
    },
    handleSave(data) {
      this.loading = true
      const param = { agmbid:this.agmbid,cont:data.tempItems }
      updateAgenDymb(param).then(response => {

        this.$alert(response.msg, '提示', {
          confirmButtonText: '关闭',
          callback: action => {
            //window.close()
          }
        })
        /* setTimeout(()=>{
          window.close()
        },2000) */
      })
    },
    // 手动初始话模板数据
    initTemp(){
      this.$refs.printDesigner.initTemp(this.value,this.widgets)
    }
  },
}
</script>
